#encoding:utf-8

from exts import db
from datetime import datetime


class BannerModel(db.Model):
    __tablename__ = 'banner'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(200),nullable=False)
    image_url = db.Column(db.String(255),nullable=False)
    link_url = db.Column(db.String(255),nullable=False)
    priority = db.Column(db.Integer,default=0)
    create_time = db.Column(db.DATETIME,default=datetime.now)


class BoardModel(db.Model):
    __tablename__ = 'board'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(255),nullable=False)
    create_time = db.Column(db.DATETIME,default=datetime.now)

# 帖子模型定义
class PostModel(db.Model):
    __tablename__ = 'post'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title = db.Column(db.String(255),nullable=False)
    content = db.Column(db.Text,nullable=False)
    create_time = db.Column(db.DATETIME,default=datetime.now)
    read_count = db.Column(db.Integer,default=0)
    #外键，关系
    board_id = db.Column(db.Integer,db.ForeignKey('board.id'))

    #一对多关系，一个版块有多个帖子posts，帖子属于哪个版块？
    board = db.relationship('BoardModel',backref='posts')

    #作者
    author_id = db.Column(db.String(100),db.ForeignKey('front_user.id'))
    #ＯＲＭ层面的关系
    author = db.relationship('FrontUser',backref='posts')
class CommentModel(db.Model):
    __tablename__ = 'comment'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    content = db.Column(db.Text,nullable=False)
    create_time = db.Column(db.DATETIME,default=datetime.now)

    post_id = db.Column(db.Integer,db.ForeignKey('post.id'))
    author_id = db.Column(db.String(100),db.ForeignKey('front_user.id'))
    origin_comment_id = db.Column(db.Integer,db.ForeignKey('comment.id'))

    post = db.relationship('PostModel',backref='comments')
    author = db.relationship('FrontUser',backref='comments')
    origin_comment = db.relationship('CommentModel',backref='replys',remote_side=[id])

#帖子加精模型
class HighlightPostModel(db.Model):
    __tablename__ = 'highlight_post'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    post_id = db.Column(db.Integer,db.ForeignKey('post.id'))
    create_time = db.Column(db.DATETIME,default=datetime.now)

    post = db.relationship('PostModel',backref='highlight')

#给帖子加火模型
class FirePostModel(db.Model):
    __tablename_ = 'fire_post'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
    create_time = db.Column(db.DATETIME, default=datetime.now)

    post = db.relationship('PostModel',backref='firelag')